package com.stadium.handler;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.stadium.bean.Place;
import com.stadium.servlet.PlaceService;
import com.stadium.tools.ResultMessage;

@Controller
@RequestMapping(value = "/views")
public class PlaceHandler {

	@Autowired
	private PlaceService placeService;

	private final Logger log = LoggerFactory.getLogger(PlaceHandler.class);

	/*
	 * 获取所有的场地
	 */
	@RequestMapping(value = "/findAllPlaces", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findAllPlaces() {
		log.info("获取所有的场地");
		try {
			ResultMessage result = new ResultMessage();
			List<Place> page = placeService.findAll();
			System.out.println("-------------------page:" + page);
			result.getResultParm().put("places", page);
			result.setResultInfo("获取场地成功");
			return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityException("查询失败");
		}
	}

	/*
	 * 通过搜索名字获取所有的场地
	 */
	@RequestMapping(value = "/findAllByName", produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findAllByName(String name) {
		log.info("获取所有的场地");
		try {
			System.out.println(name);
			ResultMessage result = new ResultMessage();
			result.getResultParm().put("places", placeService.findAllByName(name));
			System.out.println("placeService.findAllByName(name):" + placeService.findAllByName(name));
			result.setResultInfo("获取场地成功");
			return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityException("查询失败");
		}
	}
	
	
	/*
	 * 添加新的场地
	 */
	@RequestMapping(value = "/addPlaceHandler", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> addPlace(Place place) {
		log.info("添加新的场地");
		ResultMessage result = new ResultMessage();
		try {
			placeService.addPlace(place);
			result.setResultInfo("添加新的场地成功");
			return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityException("添加失败");
		}
	}

	/*
	 * 更新新的场地
	 */
	@RequestMapping(value = "/updataPlace", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> updataPlace(Place place) {
		log.info("更新场地");
		ResultMessage result = new ResultMessage();
		try {
			placeService.updataPlace(place);
			result.setResultInfo("更新场地成功");
			return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityException("更新失败");
		}
	}

	/*
	 * 删除场地操作
	 */
	@RequestMapping(value = "/deletePlaceHandler", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> deletePlace(Integer placeId) {
		log.info("删除场地操作");
		ResultMessage result = new ResultMessage();
		try {
			placeService.deletePlace(placeId);
			result.setResultInfo("删除场地成功");
			return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityException("删除失败");
		}
	}

}
